<?php
/**
 * 个人中心
 *
 * User: kaikaic
 * Date: 2018/10/26
 * Time: 17:55
 */

namespace app\api\controller;

use app\exam\service\AccountService;
use controller\BasicApi;

class Me extends BasicApi {

    public function index() {
        //TODO
    }

    /**
     * 基本信息
     */
    public function info() {
        $accountID = intval($this->getParams("uid"));           // 用户ID
        $realName = $this->getParams("real_name");              // 姓名
        $identityCard = $this->getParams("id_card");            // 身份证
        $gender = intval($this->getParams("gender"));           // 性别，1:男,2:女
        $birthday = $this->getParams("birthday");               // 出生日期
        $married = intval($this->getParams("married"));         // 0:未婚,1:已婚

        if (!$accountID || !$realName || !$identityCard) {
            $this->outputJsonError("参数错误", 401);
        }

        // 参数过滤
        if (!$this->isIdCard($identityCard)) {
            $this->outputJsonError("身份证号码错误", 403);
        }
        if (!$this->isDate($birthday)) {
            $this->outputJsonError("出生日期格式错误", 404);
        }

        $account = AccountService::getInstance()->find($accountID);
        if (!$account) {
            $this->outputJsonError("没有这个账号", 402);
        }

        $params1 = array();
        $params1['real_name'] = $realName;
        $params1['gender'] = $gender;
        $params1['identity_card'] = $identityCard;
        AccountService::getInstance()->update($params1, $accountID);

        $params2 = array();
        if ($birthday) {
            $params2['birthday'] = $birthday;
        }
        if ($married) {
            $params2['married'] = $married;
        }
        if ($params2) {
            AccountService::getInstance()->update($params2, $accountID, 'account_id', 1);
        }

        $this->outputJsonSuccess("操作完成");
    }

    private function getParams($param, $default="") {
//        return $this->request->post($param, $default);
        return $this->request->get($param, $default);
    }

    private function isDate($data) {
        if (date("Y-m-d", strtotime($data)) == $data) {
            return true;
        }else {
            return false;
        }
    }

    /**
     * 身份证是否正确
     * @param $number
     * @return bool
     */
    public static function isIdCard($number) {
        $len = strlen($number);
        if ($len != 18) {
            return false;
        }

        $number = strtoupper($number);
        //加权因子
        $wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        //校验码串
        $ai = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
        //按顺序循环处理前17位
        $sigma = 0;
        for ($i = 0;$i < 17;$i++) {
            //提取前17位的其中一位，并将变量类型转为实数
            $b = (int) $number{$i};
            //提取相应的加权因子
            $w = $wi[$i];
            //把从身份证号码中提取的一位数字和加权因子相乘，并累加
            $sigma += $b * $w;
        }
        //计算序号
        $sNumber = $sigma % 11;

        //按照序号从校验码串中提取相应的字符。
        $check_number = $ai[$sNumber];
        if ($number{17} == $check_number) {
            return true;
        } else {
            return false;
        }
    }

}

